/**
 * @className Goods_detailController
 * @author liuxuelin
 * @Date 2021/1/20
 * @version v1.0
 */

package com.hidt.shop.controller;

import com.hidt.shop.domain.GoodsAndGoods_detail;
import com.hidt.shop.domain.Goods_detail;
import com.hidt.shop.service.Goods_detailService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class Goods_detailController {
    @Autowired
    private Goods_detailService goods_detailService;

    /**
     *@方法功能描述：添加商品的详细信息
     *@methodName addGoods_detail
     *@params [goods_detail, img, req]
     *@return java.lang.String
     *@author 刘雪林
     *@Date 2021/1/20
     */
    @RequestMapping("/goods_detail/add")
    @ResponseBody
    public String addGoods_detail(Goods_detail goods_detail ,MultipartFile[] img , HttpServletRequest req) throws IOException {
        System.out.println("运行中");
        System.out.println(img);
        List<String> fileNameList = new ArrayList<>();
        for (MultipartFile mf:img){
            System.out.println(mf);
            if(!mf.isEmpty()){
                // 指定文件上传后的物理位置
                String path = "F:\\培训笔记\\第二次项目\\img";
                // 获取文件名
                String filename = mf.getOriginalFilename();
                //新的名字
                String new_file_name = UUID.randomUUID() +"-"+ filename;
                fileNameList.add(new_file_name);
                File f = new File(path, new_file_name);
                if (!f.exists()) {
                    // 上传文件
                    FileUtils.copyInputStreamToFile(mf.getInputStream(), new File(req.getServletContext().getRealPath("/images/") + new_file_name));
                    mf.transferTo(f);
                }
            }
        }
        goods_detail.setPic(fileNameList.get(0));
        goods_detail.setSmall_pic1(fileNameList.get(1));
        goods_detail.setSmall_pic2(fileNameList.get(2));
        goods_detail.setSmall_pic3(fileNameList.get(3));
        goods_detail.setSmall_pic4(fileNameList.get(4));
        goods_detail.setSmall_pic5(fileNameList.get(5));
        int i = goods_detailService.addGoods_detail(goods_detail);
        if(i > 0){
            return "商品详情信息添加成功";
        }
        return "商品详情信息添加失败";
    }

    /**
     *@方法功能描述：根据商品的id查询商品的详细信息
     *@methodName findAll
     *@params [map]
     *@return com.hidt.shop.domain.GoodsAndGoods_detail
     *@author 刘雪林
     *@Date 2021/1/20
     */
    @RequestMapping("/goods_detail/findAll")
    @ResponseBody
    public GoodsAndGoods_detail findAll(@RequestBody Map<String,String> map){
        int good_id = Integer.parseInt(map.get("good_id"));
        GoodsAndGoods_detail all = goods_detailService.findAll(good_id);
        System.out.println(all);
        return all;

    }
    /**
     *@方法功能描述：根据商品的id修改商品的详细信息
     *@methodName updateGoods_detail
     *@params [goods_detail]
     *@return java.lang.String
     *@author 刘雪林
     *@Date 2021/1/20
     */
    @RequestMapping("/goods_detail/update")
    @ResponseBody
    public String updateGoods_detail(@RequestBody Goods_detail goods_detail){
        int i = goods_detailService.updateGoods_detailAll(goods_detail);
        if (i>0) {
            return "修改成功1";
        }else{
            return "修改失败0";
        }
    }
    /**
     *@方法功能描述：浏览量的增加（访问商品自动添加）
     *@methodName updateGoodsWatch
     *@params [map]
     *@return void
     *@author 刘雪林
     *@Date 2021/1/21
     */
    @RequestMapping("/goods_detail/updatebyid")
    @ResponseBody
    public String updateGoodsWatch(@RequestBody Map<String,String> map){
        System.out.println("工作中");
        int good_id = Integer.parseInt(map.get("good_id"));
        goods_detailService.updateGoodsWatch(good_id);
        return "浏览量已经加一";
    }
    /**
     *@方法功能描述：查询所有的商品
     *@methodName findAll
     *@params []
     *@return java.util.List<com.hidt.shop.domain.Goods_detail>
     *@author 刘雪林
     *@Date 2021/1/26
     */
    @RequestMapping("/goods_detail/find")
    @ResponseBody
    public List<Goods_detail> findAll(){
        List<Goods_detail> allGoods = goods_detailService.findAllGoods();
        return allGoods;
    }

    @RequestMapping("/goods_detail/findbyid")
    @ResponseBody
    public List<Goods_detail> findGood_id(@RequestBody Map<String,String> map){
        int good_id=Integer.parseInt(map.get("good_id"));
        List<Goods_detail> goodById = goods_detailService.findGoodById(good_id);
        return goodById;
    }
}
